/*
  Copyright (c) 2009 Barry Schwartz

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*/

/*-----------------------------------------------------------------------*/
#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
#include <stdio.h>
#include <kpathsea/kpathsea.h>

/*-----------------------------------------------------------------------*/

CAMLprim value kpse_init_prog_wrapper(value name, value dpi, value mode, value alt_font)
{
  CAMLparam4(name, dpi, mode, alt_font);

  kpse_init_prog(String_val(name), Int_val(dpi), String_val(mode), String_val(alt_font));
  CAMLreturn(Val_unit);
}

CAMLprim value kpse_set_program_name_wrapper(value argv0, value progname)
{
  CAMLparam2(argv0, progname);

  kpse_set_program_name(String_val(argv0), String_val(progname));
  CAMLreturn(Val_unit);
}

CAMLprim value
kpse_set_program_enabled_wrapper(value fmt, value val, value level)
{
    CAMLparam3(fmt, val, level);

    kpse_set_program_enabled(Int_val(fmt), Int_val(val), Int_val(level));
    CAMLreturn(Val_unit);
}

CAMLprim value
kpse_find_file_wrapper(value name, value format, value must_exist)
{
    CAMLparam3(name, format, must_exist);
    CAMLlocal1(result);

    char *filename = kpse_find_file(String_val(name), Int_val(format),
                                    Bool_val(must_exist));
    if (filename == NULL)
        result = caml_copy_string("");
    else
        {
            result = caml_copy_string(filename);
            free(filename);
        }
    CAMLreturn(result);
}

CAMLprim value
kpse_find_glyph_wrapper(value name, value dpi, value format)
{
    CAMLparam3(name, dpi, format);
    CAMLlocal1(result);
    kpse_glyph_file_type glyph_file;

    char *filename = kpse_find_glyph(String_val(name), Int_val(dpi),
                                     Int_val(format), &glyph_file);
    if (filename == NULL)
        result = caml_copy_string("");
    else
        {
            result = caml_copy_string(filename);
            free(filename);
        }
    CAMLreturn(result);
}

/*-----------------------------------------------------------------------*/

CAMLprim value
kpse_var_value_wrapper(value var)
{
    CAMLparam1(var);
    CAMLlocal1(result);

    char *s = kpse_var_value(String_val(var));

    if (s == NULL)
        caml_raise_not_found();
    result = caml_copy_string(s);
    free(s);
    CAMLreturn(result);
}

CAMLprim value
kpse_var_expand_wrapper(value src)
{
    CAMLparam1(src);
    CAMLlocal1(result);

    char *s = kpse_var_expand(String_val(src));

    if (s == NULL)
        caml_raise_not_found();
    result = caml_copy_string(s);
    free(s);
    CAMLreturn(result);
}

/*-----------------------------------------------------------------------*/

CAMLprim value
kpse_expansion_function_wrapper(string (*funct)(const_string), value s)
{
    CAMLparam1(s);
    CAMLlocal1(result);
    
    char *expansion = (*funct)(String_val(s));

    if (expansion == NULL)
        caml_raise_out_of_memory();
    result = caml_copy_string(expansion);
    free(expansion);
    CAMLreturn(result);
}

CAMLprim value
kpse_expand_wrapper(value s)
{
    return kpse_expansion_function_wrapper(kpse_expand, s);
}

CAMLprim value
kpse_brace_expand_wrapper(value s)
{
    return kpse_expansion_function_wrapper(kpse_brace_expand, s);
}

CAMLprim value
kpse_path_expand_wrapper(value s)
{
    return kpse_expansion_function_wrapper(kpse_path_expand, s);
}

/*-----------------------------------------------------------------------*/

CAMLprim value
kpse_path_search_wrapper(value path, value name, value must_exist)
{
    CAMLparam3(path, name, must_exist);
    CAMLlocal1(result);
    
    char *file = kpse_path_search(String_val(path), String_val(name),
                                  Bool_val(must_exist));

    if (file == NULL)
        file = strdup("");
    result = caml_copy_string(file);
    free(file);
    CAMLreturn(result);
}

/*-----------------------------------------------------------------------*/
